import http from './request.js'
/**
 * 微信支付
 */
export function wxPay(param) {
  return new Promise((resolve, reject) => {
    wx.requestPayment({
      'timeStamp': param.obj.timeStamp,
      'nonceStr': param.obj.nonceStr,
      'package': param.obj.package,
      'signType': 'MD5',
      'paySign': param.obj.paySign,
      'success': function (res) {
        console.log(res);
        resolve(res)
      },
      'fail': function (res) {
        reject(res)
      }
    })
  });
}

/**
 * 用户授权
 */
export function charge({
  money,
}) {

  return http.get('money/recharge', {
    total: money
  });
}

/**
 * 用户授权
 */
export function buy({
  jsonKey,
  jsonVal,
}) {
  // if (!jsonVal) {
    jsonVal = jsonVal||JSON.stringify({
      [jsonKey]: '1'
    })
  // }
  const params = {
    jsonKey,
    jsonVal,
  }
  let shareId= wx.getStorageSync('shareId')
  if(shareId){
    params.shareId = shareId
  }



  return http.post('order/newAddOrder', params);
}

/**
 * 用户授权
 */
export function buyCart({
  jsonKey,
  jsonVal,
}) {
  const params = {
    jsonKey,
    jsonVal,
  }
  // jsonKey = '1500438897300123650'
  // const jsonVal = JSON.stringify({
  //     [jsonKey]  : '1'
  //   })
  if(wx.getStorageSync('shareId')){
    params.shareId = shareId
  }
  return http.post('order/addOrder', params);
}